package drk.com.coincounter;



import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;

import android.content.Context;
import android.content.Intent;

import android.util.Log;

import android.widget.RemoteViews;


public class CoinCounterWidget extends AppWidgetProvider {
	public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static final String URI_SCHEME = "images_widget";
    @Override
    public void onEnabled(Context context) {
    	super.onEnabled(context);
    	
        Log.d("Coin", "onEnabled");
        // When the first widget is created, register for the TIMEZONE_CHANGED and TIME_CHANGED
        // broadcasts.  We don't want to be listening for these if nobody has our widget active.
        // This setting is sticky across reboots, but that doesn't matter, because this will
        // be called after boot if there is a widget instance for this provider.
    }

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
	int[] appWidgetIds) {	
		final int N = appWidgetIds.length;      
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            Log.v("Coin","Onupdate Appwidget id being sent is " + appWidgetId);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    		Intent configIntent = new Intent(context, CoinCounterActivity.class);
    		configIntent.setAction(ACTION_WIDGET_CONFIGURE);
    		configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		
    		configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    		PendingIntent configPendingIntent = PendingIntent.getActivity(context, appWidgetId, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    		remoteViews.setOnClickPendingIntent(R.id.widgetwhole, configPendingIntent);
    		remoteViews.setOnClickPendingIntent(R.id.widget_textview, configPendingIntent);
    		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
            
        }

		//Timer timer = new Timer();
		//timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
	}
	 @Override
    public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
    	Log.v("Coin", "action is: " + intent.getAction());
     super.onReceive(context, intent);

    }


	static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, String titlePrefix) {
		
        Log.d("Coin", "updateAppWidget appWidgetId=" + appWidgetId + " titlePrefix=" + titlePrefix);
        // Getting the string this way allows the string to be localized.  The format
        // string is filled in using java.util.Formatter-style format strings.
        //CharSequence text = context.getString(R.string.appwidget_text_format,
         //       ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId),
          //      "0x" + Long.toHexString(SystemClock.elapsedRealtime()));
        CharSequence text = titlePrefix;
        // Construct the RemoteViews object.  It takes the package name (in our case, it's our
        // package, but it needs this because on the other side it's the widget host inflating
        // the layout from our package).
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
        views.setTextViewText(R.id.widget_textview, text);
        
        Intent configIntent = new Intent(context, CoinCounterActivity.class);
		configIntent.setAction(ACTION_WIDGET_CONFIGURE);
		configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		PendingIntent configPendingIntent = PendingIntent.getActivity(context, appWidgetId, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.widgetwhole, configPendingIntent);
		views.setOnClickPendingIntent(R.id.widget_textview, configPendingIntent);
		
        // Tell the widget manager
        int[] ar = new int[1];
        ar[0] = appWidgetId;
        appWidgetManager.updateAppWidget(appWidgetId, views);
        
    }
	

			}
	

